import axios from 'axios'
import { Message } from 'element-ui';
/* import { Toast } from 'vant'
import { isPC } from '@/plugins/util.js' */
import { base } from '@/conf'
import Qs from 'qs'
axios.defaults.withCredentials = true // 让ajax携带cookie
// Access-Control-Allow-Origin
console.log(base)
const service = axios.create({// 创建 axios 实例
  baseURL: base.baseUrl,
  timeout: 0 ,// 请求超时时间
  withCredentials: false, // default
})

import store from '@/store'
service.interceptors.request.use(request => {
	const token = store.getters.token
	if (token && token!= undefined)
		request.headers['SRC-TOKEN'] = token
  if(/\/upload\/./.test(request.url))//文件相关
    request.headers['Content-Type'] = 'multipart/form-data'
  return request
},
err=>{
  return Promise.reject(err.data)
})
//拦截返回值
service.interceptors.response.use(
  response=> {
    const res = response.data
    if (res.code == 200)
        return Promise.resolve(res.data)
    else {
        Message.error(res.msg)
        return Promise.reject(res)
    }
  },
  err=>{
    const { status:code } = err.request
    if(code == 404)
      Message.error('接口未找到')
    return Promise.reject(err)
  }
)
export default service